package wxpay;

import android.content.Context;
import android.text.TextUtils;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class WXPay {

    public static String APP_ID = "";

    public IWXAPI api;
    private PayReq req;
    private Context context;

    public WXPay(Context context) {
        this.context = context;
        this.api = WXAPIFactory.createWXAPI(context, APP_ID,false);
        api.registerApp(WXPay.APP_ID);
    }

    /**
     * 向微信服务器发起的支付请求
     */
    public void pay(String partnerid, String prepayid, String nonceStr, String timeStamp, String sign) {

        req = new PayReq();

        req.appId = APP_ID;
        req.partnerId = partnerid;//    商户号
        req.prepayId = prepayid;//  预付款ID
        req.nonceStr = nonceStr;//getRoundString();//随机数
        req.timeStamp = timeStamp;// getTimeStamp();//时间戳
        req.packageValue = "Sign=WXPay";//固定值Sign=WXPay

        req.sign = sign;//getSign();//签名
        req.extData = "";

//        api.registerApp("wxAPPID");
        api.sendReq(req);
    }

    private String getSign() {
        Map<String, String> map = new HashMap<>();
        map.put("appid", req.appId);
        map.put("partnerid", req.partnerId);
        map.put("prepayid", req.prepayId);
        map.put("package", req.packageValue);
        map.put("noncestr", req.nonceStr);
        map.put("timestamp", req.timeStamp);

        ArrayList<String> sortList = new ArrayList<>();
        sortList.add("appid");
        sortList.add("partnerid");
        sortList.add("prepayid");
        sortList.add("package");
        sortList.add("noncestr");
        sortList.add("timestamp");
        sort(sortList);

        String md5 = "";
        int size = sortList.size();
        for (int k = 0; k < size; k++) {
            if (k == 0) {
                md5 += sortList.get(k) + "=" + map.get(sortList.get(k));
            } else {
                md5 += "&" + sortList.get(k) + "=" + map.get(sortList.get(k));
            }
        }
        String stringSignTemp = md5 + "&key=商户密钥";

        String sign = md5(stringSignTemp).toUpperCase();

        return sign;
    }

    public static String md5(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    private String getRoundString() {

        Random random = new Random();

        return random.nextInt(10000) + "";
    }

    private String getTimeStamp() {
        return new Date().getTime() / 10 + "";
    }


    private static void sort(ArrayList<String> strings) {
        Collections.sort(strings);
    }
}